Shader "Custom/卡通(带影子)"
{
    Properties
    {
        _Color("Color", Color) = (1, 1, 1, 1)
        _HColor("Highlight Color", Color) = (0.8, 0.8, 0.8, 1.0)
        _SColor("Shadow Color", Color) = (0.2, 0.2, 0.2, 1.0)
        _MainTex("Main Texture", 2D) = "white" {}
        _RampThreshold("Ramp Threshold", Range(0, 1)) = 0.5
        _RampSmooth("Ramp Smooth", Range(0, 1)) = 0.1
        _OutlineColor("Outline Color", Color) = (0, 0, 0, 1)
        _OutlineWidth("Outline Width", Float) = 0.05
        _Brightness("Brightness", Range(1.0, 2.0)) = 1.2
        _HeightLightFactor("Height Light Factor", Range(0.0, 10.0)) = 2.0
        _HeightMin("Height Min", Float) = 0.0
        _HeightMax("Height Max", Float) = 5.0
        _GroundY("影子高度", float) = 0
        _Shadow_Color("影子颜色", Color) = (0.25, 0.25, 0.25, 1)
        _Shadow_Length("影子长度", float) = 1
        _Shadow_Rotated("影子旋转角度", Range(0, 360)) = 45
    }

    SubShader
    {
        Tags { "RenderType" = "Opaque" "Queue" = "Geometry+1" }
        LOD 100

        Pass
        {
            Name "BASE"
            Tags { "LightMode" = "ForwardBase" }
            Blend SrcAlpha oneMinusSrcAlpha
            Offset -2, -2
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            struct appdata
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
                float3 worldNormal : TEXCOORD1;
                float3 worldPos : TEXCOORD2;
            };

            sampler2D _MainTex;
            fixed4 _Color;
            fixed4 _HColor;
            fixed4 _SColor;
            float _RampThreshold;
            float _RampSmooth;
            float _Brightness;
            float _HeightLightFactor;
            float _HeightMin;
            float _HeightMax;

            v2f vert(appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                o.worldNormal = UnityObjectToWorldNormal(v.normal);
                return o;
            }

            void frag(v2f i, out float4 color : SV_Target)
            {
                fixed4 tex = tex2D(_MainTex, i.uv);
                fixed3 lightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
                float ndotl = dot(i.worldNormal, lightDir);

                float rampFactor = smoothstep(_RampThreshold - _RampSmooth, _RampThreshold + _RampSmooth, ndotl);
                float heightFactor = smoothstep(_HeightMin, _HeightMax, i.worldPos.y) * _HeightLightFactor;

                fixed3 finalColor = lerp(_SColor.rgb, _HColor.rgb, rampFactor + heightFactor);
                color.rgb = (tex.rgb * _Color.rgb * finalColor) * _Brightness;
                color.a = tex.a;
            }
            ENDCG
        }

        Pass
        {
            Name "SHADOW"
            Stencil
            {
                Ref 1
                Comp Greater
                Pass Replace
            }
            Blend SrcAlpha oneMinusSrcAlpha
            Offset -2, -2
            CGPROGRAM
            #pragma vertex vert_shadow
            #pragma fragment frag_shadow
            #include "UnityCG.cginc"
            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float4 worldPos : TEXCOORD0;
                float cacheWorldY : TEXCOORD1;
            };

            half _GroundY;
            half4 _Shadow_Color;
            half _Shadow_Length;
            half _Shadow_Rotated;

            v2f vert_shadow(appdata v)
            {
                v2f o = (v2f)0;
                o.worldPos = mul(unity_ObjectToWorld, v.vertex);
                o.cacheWorldY = o.worldPos.y;
                o.worldPos.y = _GroundY;

                half lerpVal = lerp(0, _Shadow_Length, o.cacheWorldY - _GroundY);
                half radian = _Shadow_Rotated / 180.0 * UNITY_PI;
                half2 rotatedAngle = half2((0 * cos(radian) - 1 * sin(radian)), (0 * sin(radian) + 1 * cos(radian)));
                o.worldPos.xz += lerpVal * rotatedAngle;

                o.pos = mul(UNITY_MATRIX_VP, o.worldPos);
                return o;
            }

            fixed4 frag_shadow(v2f i) : SV_Target
            {
                clip(i.cacheWorldY - _GroundY);
                return _Shadow_Color;
            }

            ENDCG
        }
    }
    FallBack "Diffuse"
}
